// SelectableButtonCell.h
#import <UIKit/UIKit.h>
#import <Masonry/Masonry.h>
#import "XMCommentViewCell.h"

@protocol XMCommentViewCellDelegate <NSObject>
- (void)cellButtonDidTap:(XMCommentViewCell *)cell;
@end

@interface XMCommentViewCell()
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, weak) id<XMCommentViewCellDelegate> delegate;
@property (nonatomic, assign) BOOL isSelected;
@end

// SelectableButtonCell.m

@implementation XMCommentViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    UIView * mainView = [UIView new];
//    [self.contentView addSubview:mainView];
//    
//    [mainView mas_makeConstraints:^(MASConstraintMaker *make) {
////        make.edges.equalTo(self.contentView).inset(0);
//       make.left.equalTo(self.contentView).offset(2);
////       make.top.equalTo(self.contentView).inset(2);
//     }];
//    mainView.backgroundColor = kColorHEX(0xB8B8B8);
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button.layer.cornerRadius = 12;
    self.button.layer.masksToBounds = YES;
    [self.button setTitle:@"全部36万+" forState:UIControlStateNormal];
    [self.button setTitleColor:kColorHEX(0x3C76EB) forState:UIControlStateNormal];
    self.button.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.button addTarget:self action:@selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.button];
    
    // 使用Masonry布局按钮（需导入Masonry库）
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).inset(2);
      make.left.and.right.equalTo(self.contentView).offset(2);
        make.top.equalTo(self.contentView).offset(10);

    }];
    
    // 默认样式
    [self updateButtonStyle];
}

- (void)buttonTapped {
    if (self.delegate && [self.delegate respondsToSelector:@selector(cellButtonDidTap:)]) {
        [self.delegate cellButtonDidTap:self];
    }
}

- (void)setIsSelected:(BOOL)isSelected {
    _isSelected = isSelected;
    [self updateButtonStyle];
}

- (void)updateButtonStyle {
    if (self.isSelected) {
        [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.button.backgroundColor = [UIColor colorWithRed:0.2 green:0.6 blue:0.9 alpha:1.0]; // 选中时蓝色
    } else {
        [self.button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        self.button.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0]; // 未选中时浅灰色
    }
}

@end
